package com.jws.jawestshow;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.jws.jawestshow.config.ItemConfig;
import com.jws.jawestshow.config.Properties;
import com.jws.thirdlibrary.CxManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


public class MainActivity extends Activity {

    private GridView MustCheckgirdView;
    private SimpleAdapter mustCheckAdapter;
    public static Map<String, String> stringmapkey;
    private CxManager mCxManager = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stringmapkey = new HashMap<String, String>();
        MustCheckgirdView = (GridView) findViewById(R.id.main_mustCheck_girdView);
        mCxManager = CxManager.create(this);
    }

    private void updateGirdView() {
        LinkedList<String> mustChecklist = (LinkedList<String>) new ItemConfig().getArea1LinkedList(ItemConfig.AllItems);

        stringmapkey.clear();

        ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        for (int j = 0; j < mustChecklist.size(); j++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            String Itemkey = mustChecklist.get(j);
            String ItemStr = getString(ItemConfig.getKeyStringID(Itemkey));

            stringmapkey.put(ItemStr, Itemkey);

            map.put("ItemTest", ItemStr);
            map.put("ItemIamge", ItemConfig.getKeyimageID(Itemkey));
            list.add(map);
        }

        mustCheckAdapter = new SimpleAdapter(this, list, R.layout.item_view_main,
                new String[]{"ItemIamge", "ItemTest"}, new int[]{
                R.id.item_image, R.id.item_text});
        MustCheckgirdView.setAdapter(mustCheckAdapter);
        MustCheckgirdView.setOnItemClickListener(mustListener);

    }

    OnItemClickListener mustListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            HashMap<String, Object> item = (HashMap<String, Object>) mustCheckAdapter.getItem(position);
            String stringvalue = (String) item.get("ItemTest");
            String itemvalue = stringmapkey.containsKey(stringvalue) ? stringmapkey.get(stringvalue) : null;

            if (Properties.KEY_CAMERA.equals(itemvalue)) {
                int NumberOfCameras = Camera.getNumberOfCameras();
                if (NumberOfCameras > 1) {
                    startActivity(new Intent(MainActivity.this, DoubleCameraActivity.class));
                } else if (NumberOfCameras == 1) {
                    startActivity(new Intent(MainActivity.this, CameraActivity.class));
                } else {
                    Toast.makeText(MainActivity.this, R.string.jws_nocamera, Toast.LENGTH_SHORT).show();
                }
                return;
            }

            if (null != ItemConfig.getMapClass(itemvalue)) {
                startActivity(new Intent(MainActivity.this, ItemConfig.getMapClass(itemvalue)));
            }
        }
    };


    @Override
    protected void onResume() {
        updateGirdView();
        super.onResume();
    }

}
